package com.maimang.platform.common.service.impl;

import com.maimang.platform.common.entity.SysDictType;
import com.maimang.platform.common.mapper.SysDictDataMapper;
import com.maimang.platform.common.mapper.SysDictTypeMapper;
import com.maimang.platform.common.service.SysDictTypeService;
import com.maimang.platform.constant.UserConstants;
import com.maimang.platform.exception.BusinessException;
import com.maimang.platform.lang.ConvertUtils;
import com.maimang.platform.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典 业务层处理
 *
 * @author Rocky
 */
@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {
	@Autowired
	private SysDictTypeMapper sysDictTypeMapper;

	@Autowired
	private SysDictDataMapper sysDictDataMapper;

	/**
	 * 根据条件分页查询字典类型
	 *
	 * @param dictType 字典类型信息
	 * @return 字典类型集合信息
	 */
	@Override
	public List<SysDictType> selectDictTypeList(SysDictType dictType) {
		return sysDictTypeMapper.selectDictTypeList(dictType);
	}

	/**
	 * 根据所有字典类型
	 *
	 * @return 字典类型集合信息
	 */
	@Override
	public List<SysDictType> selectDictTypeAll() {
		return sysDictTypeMapper.selectDictTypeAll();
	}

	/**
	 * 根据字典类型ID查询信息
	 *
	 * @param dictId 字典类型ID
	 * @return 字典类型
	 */
	@Override
	public SysDictType selectDictTypeById(Long dictId) {
		return sysDictTypeMapper.selectDictTypeById(dictId);
	}

	/**
	 * 通过字典ID删除字典信息
	 *
	 * @param dictId 字典ID
	 * @return 结果
	 */
	@Override
	public int deleteDictTypeById(Long dictId) {
		return sysDictTypeMapper.deleteDictTypeById(dictId);
	}

	/**
	 * 批量删除字典类型
	 *
	 * @param ids 需要删除的数据
	 * @return 结果
	 */
	@Override
	public int deleteDictTypeByIds(String ids) throws Exception {
		Long[] dictIds = ConvertUtils.toLongArray(ids);
		for (Long dictId : dictIds) {
			SysDictType dictType = selectDictTypeById(dictId);
			if (sysDictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
				throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
			}
		}

		return sysDictTypeMapper.deleteDictTypeByIds(dictIds);
	}

	/**
	 * 新增保存字典类型信息
	 *
	 * @param dictType 字典类型信息
	 * @return 结果
	 */
	@Override
	public int insertDictType(SysDictType dictType) {
		return sysDictTypeMapper.insertDictType(dictType);
	}

	/**
	 * 修改保存字典类型信息
	 *
	 * @param dictType 字典类型信息
	 * @return 结果
	 */
	@Override
	public int updateDictType(SysDictType dictType) {
		SysDictType oldDict = sysDictTypeMapper.selectDictTypeById(dictType.getDictId());
		sysDictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
		return sysDictTypeMapper.updateDictType(dictType);
	}

	/**
	 * 校验字典类型称是否唯一
	 *
	 * @param dict 字典类型
	 * @return 结果
	 */
	@Override
	public String checkDictTypeUnique(SysDictType dict) {
		Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
		SysDictType dictType = sysDictTypeMapper.checkDictTypeUnique(dict.getDictType());
		if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) {
			return UserConstants.DICT_TYPE_NOT_UNIQUE;
		}
		return UserConstants.DICT_TYPE_UNIQUE;
	}
}
